#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PATH "."
#define FILENAME "fifo"
#define ERR_EXIT(M)         \
    do                      \
    {                       \
        perror(M);          \
        exit(EXIT_FAILURE); \
    } while (0);

class FifoName
{
public:
    FifoName(const std::string &path, const std::string &name) // 创建命名管道
        : _path(path), _name(name)
    {
        umask(0);
        _fifoname = _path + "/" + _name;
        int n = mkfifo(_fifoname.c_str(), 0666);
        if (n < 0)
        {
            ERR_EXIT("mkfifo");
        }
        else
        {
            std::cout << "mkfifo success" << std::endl;
        }
    }
    ~FifoName() // 删除管道文件
    {
        int n = unlink(_fifoname.c_str());
        if (n != 0)
        {
            ERR_EXIT("mkfifo");
        }
        else
        {
            std::cout << "remove fifo failed" << std::endl;
        }
    }

private:
    std::string _path;
    std::string _name;
    std::string _fifoname;
};
class FileOpen
{
public:
    FileOpen(const std::string &path, const std::string &name)
        : _path(path), _name(name),_fd(-1)
    {
        _fifoname = _path + "/" + _name;
    }
    void OpenForRead()
    {
        _fd = open(_fifoname.c_str(), O_RDONLY);
        if (_fd < 0)
        {
            ERR_EXIT("open");
        }
        else
        {
            std::cout << "open fifo success" << std::endl;
        }
    }
    void OpenForWrite()
    {
        _fd = open(_fifoname.c_str(), O_WRONLY);
        if (_fd < 0)
        {
            ERR_EXIT("open");
        }
        else
        {
            std::cout << "open fifo success" << std::endl;
        }
    }
    void Read()
    {
        while (true)
        {
            char buffer[1024];
            int n = read(_fd, buffer, sizeof(buffer) - 1);
            if (n > 0)
            {
                buffer[n] = 0;
                std::cout << "Client Say# " << buffer << std::endl;
            }
            else if (n == 0)
            {
                std::cout << "client quit! me too!" << std::endl;
                break;
            }
            else
            {
                ERR_EXIT("read");
                break;
            }
        }
    }
    void Write()
    {
        std::string message;
        int cnt = 1;
        pid_t id = getpid();
        while (true)
        {
            std::cout << "Please Enter# ";
            std::getline(std::cin, message);
            message += (", message number:" + std::to_string(cnt++) + ", [" + std::to_string(id) + "]");
            write(_fd, message.c_str(), message.size());
        }
    }
    void Close()
    {
         if (_fd > 0)
            close(_fd);
    }
    ~FileOpen()
    {
    }
private:
    std::string _path;
    std::string _name;
    std::string _fifoname;
    int _fd;
};